package controllers

import (
    "github.com/astaxie/beego"
    "git.oschina.net/hfwb/tianqi.git/tq/global"
    "errors"
)

type responseData struct {
    Error   bool
    Code    int
    Data    interface{}
    Message string
}

type base struct {
    beego.Controller
    token   string
    staffId int
}

func (this *base) Response(value interface{}) {
    var data responseData
    if err, ok := value.(error); ok {
        data.Error = true
        data.Message = err.Error()
    } else {
        data.Data = value
    }
    this.Data["json"] = data
    this.ServeJSON(true)
}

func (this *base) Prepare() {
    this.token = this.Ctx.Request.Header.Get("token")
    if this.token != "" {
        this.staffId, _ = global.DeCodeId(this.token)
    }
}

func (this *base) MustLogin() bool {
    if this.staffId == 0 {
        this.Response(errors.New("must login"))
        return false
    }
    return true
}

